package com.jeecms.wicool.manager.shops.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.shops.CouponDao;
import com.jeecms.wicool.entity.shops.Coupon;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.entity.user.UserCoupon;
import com.jeecms.wicool.manager.shops.CouponMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Service
@Transactional
public class CouponMngImpl implements CouponMng {
	
	private CouponDao dao;
	@Autowired
	public void setDao(CouponDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Integer couponType,String couponName,Short status ,int pageNo, int pageSize,CmsUser user) {
		Pagination page = dao.getPage(couponType, couponName, status, pageNo, pageSize, user);
		return page;
	}
	@Override
	public Coupon findById(Integer id) {
		Coupon entity = dao.findById(id);
		return entity;
	}
	@Override
	public Coupon update(Coupon bean,Integer shopId) {
		ShopInfo shop= shopMng.findById(shopId);
		bean.setShop(shop);
		Updater<Coupon> updater = new Updater<Coupon>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}
	
	@Override
	public Coupon save(Coupon bean,Integer shopId) {
		
		ShopInfo shop= shopMng.findById(shopId);
		bean.setShop(shop);
		dao.save(bean);
		return bean;
	}

	@Override
	public Coupon deleteById(Integer id) {
		Coupon bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public Coupon[] deleteByIds(Integer[] ids) {
		Coupon[] beans = new Coupon[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Override
	public List<Coupon> getAll(CmsUser user) {
		return dao.getAll(user);
	}
	
	public List<UserCoupon> findByCouponId(Integer couponId){
		return dao.findByCouponId(couponId);
	}
	
	@Autowired
	private WiShopInfoMng shopMng;
	
}